from flask import Flask, request, jsonify
from dotenv import load_dotenv
import os
from stock_service import get_stock_fundamentals
from tools_service import use_tools
from analysis_service import analyze_stock
from chatdeepseek import analyze_stock_deepseek
from langgraphtools import analyze_stock_tools
from searchapi import SearchAPI  
from langchain.globals import set_debug

set_debug(True)
load_dotenv()

app = Flask(__name__)

# 创建 SearchAPI 类的实例
search_api = SearchAPI()

@app.route('/analyze', methods=['POST'])
def analyze():
    data = request.json
    stock_code = data.get('stock_code')
    
    try:
        # 获取股票基本面数据
        fundamentals = get_stock_fundamentals(stock_code)
        print(fundamentals)
        # 调用大模型分析
        analysis_result = analyze_stock(fundamentals)
        print(analysis_result)
        return jsonify({
            'status': 'success',
            'analysis': analysis_result
        })
    except Exception as e:
        return jsonify({
            'status': 'error',
            'message': str(e)
        }), 500

@app.route('/analyzedeepseek', methods=['POST'])
def analyzedeepseek():
    data = request.json
    stock_code = data.get('stock_code')
    
    try:
        # 获取股票基本面数据
        fundamentals = get_stock_fundamentals(stock_code)
        print(fundamentals)
        # 调用大模型分析
        analysis_result = analyze_stock_deepseek(fundamentals)
        print(analysis_result)
        return jsonify({
            'status': 'success',
            'analysis': analysis_result
        })
    except Exception as e:
        return jsonify({
            'status': 'error',
            'message': str(e)
        }), 500

@app.route('/usetools', methods=['POST'])
def usetools():
    try:
        # 调用大模型使用工具
        use_result = use_tools()
        print(use_result)
        return jsonify({
            'status': 'success',
            'result': use_result
        })
    except Exception as e:
        return jsonify({
            'status': 'error',
            'message': str(e)
        }), 500
    
@app.route('/search', methods=['POST'])
def search():
    data = request.json
    msg = data.get('msg')
    
    try:
        # 获取股票基本面数据
        res = search_api.get_serach(msg=msg)
        print(res)
       
        return jsonify({
            'status': 'success',
            'result': res
        })
    except Exception as e:
        return jsonify({
            'status': 'error',
            'message': str(e)
        }), 500
    
@app.route('/graph', methods=['POST'])
def graphtools():
    data = request.json
    stock_code = data.get('stock_code')
    
    try:
        # 调用大模型分析
        analysis_result = analyze_stock_tools(stock_code)
        print(analysis_result)
        return jsonify({
            'status': 'success',
            'analysis': analysis_result
        })
    except Exception as e:
        return jsonify({
            'status': 'error',
            'message': str(e)
        }), 500

if __name__ == '__main__':
    app.run(debug=True)
